package org.xmdl.silk.wrap;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.xmdl.silk.Invocation;
import org.xmdl.silk.Method;
import org.xmdl.silk.ValueItem;
import org.xmdl.silk.impl.InvocationImpl;
import org.xmdl.silk.visit.Visitor;

public class InvocationWrap extends InvocationImpl implements StatementWrap, ExpressionWrap {

	private final Invocation w;
	private EList<ValueItem> wParameters;

	public InvocationWrap(Invocation e) {
		this.w = e;
		method = WrapUtils.method(e.getMethod());
	}

	@Override
	public EList<ValueItem> getParameters() {
		if (wParameters == null) {
			wParameters = new BasicEList<ValueItem>();
			for (ValueItem c : w.getParameters()) {
				ValueItem cw = WrapUtils.valueItem(c);
				wParameters.add(cw);
			}
		}
		return wParameters;
	}

	@Override
	public void setMethod(Method newMethod) {
		method = WrapUtils.method(newMethod);
		w.setMethod(method);
	}

	public boolean accept(Visitor visitor) {
		if (visitor.visitEnter(this)) {
			Wrapped method = (Wrapped) getMethod();
			method.accept(visitor);
			
			EList<ValueItem> params = getParameters();
			for (ValueItem param : params) {
				Wrapped p = (Wrapped) param;
				p.accept(visitor);
			}
		}
		boolean result = visitor.visitLeave(this);
		return result;
	}

}
